<?php


namespace App\Http\Controllers\Admin\Erm;


use App\Enums\UserEnum;
use App\Jobs\QiPao\ExcelJob;
use App\Response\Response;
use App\Services\Tools\Export;
use App\Services\Tools\HelperService;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Enums\ErmEnum;

//灰名单
class GrayListController extends BaseController
{

    /**
     * @api {get} /erm/gray/list 灰名单列表
     * @apiVersion 0.1.0
     * @apiName 灰名单列表
     * @apiGroup 灰名单管理
     *
     * @apiParam {int} user_code 用户ID，非必传
     * @apiParam {int} nickname 用户昵称，非必传
     * @apiParam {int} user_type 用户类型 1普通用户 2官方管理 3公会会长 4公会厅主 5公会成员，非必传
     * @apiParam {int} min_recharge_money 最大累计充值金额，非必传
     * @apiParam {int} max_recharge_money 最小累计充值金额，非必传
     * @apiParam {int} start_register_time 最大注册时间，非必传
     * @apiParam {int} end_register_time 最小注册时间，非必传
     * @apiParam {int} start_recharge_time 最大最后充值时间，非必传
     * @apiParam {int} end_recharge_time 最小最后充值时间，非必传
     * @apiParam {int} auth_type 认证状态 1充值 2消费 3提现 4兑换，非必传
     * @apiParam {int} start_add_time 最小添加时间，非必传
     * @apiParam {int} end_add_time 最大添加时间，非必传
     * @apiParam {int} page 第几页，默认1
     * @apiParam {int} page_size 每页显示条数，1-100之间，默认10
     *
     * @apiSuccessExample {json} Response-Example
     * {
     * "code": 200,
     * "data": {
     * "current_page": 1,
     * "data": [{
     * "id": 3,
     * "nickname(用户昵称)": "小气泡8442052",
     * "user_code(用户ID)": 8442052,
     * "user_type(用户类型)": "普通用户",
     * "register_time(注册时间)": "2021-11-17 22:04:11",
     * "recharge_num(累计充值订单)": 0,
     * "recharge_money(累计充值金额)": "0.00",
     * "last_recharge_time(最后充值时间)": "2021-11-17 22:04:11",
     * "gray_num(灰名单次数)": 0,
     * "black_num(黑名单次数)": 0,
     * "register_date(注册天数)": 7,
     * "join_time(添加时间)": "2021-11-26 16:46:56",
     * "join_date(添加天数)": 1,
     * "join_user(添加人)": "admin",
     * "authentication_data(认证状态)": [
     * "消费",
     * "兑换"
     * ]
     * }],
     * "first_page_url": "http://www.qipaoadmin.com/erm/public/list?=1",
     * "from": 1,
     * "last_page": 1,
     * "last_page_url": "http://www.qipaoadmin.com/erm/public/list?=1",
     * "next_page_url": null,
     * "path": "http://www.qipaoadmin.com/erm/public/list",
     * "per_page": 20,
     * "prev_page_url": null,
     * "to": 6,
     * "total": 6
     * },
     * "message": ""
     * }
     */
    public function list(Request $request)
    {
        $param = $request->only('user_code', 'nickname', 'user_type', 'min_recharge_money', 'max_recharge_money', 'start_register_time', 'end_register_time', 'start_recharge_time', 'end_recharge_time', 'auth_type', 'start_add_time', 'end_add_time', 'page', 'page_size', 'reason');
        $v = app('validator')->make($param, [
            'user_code' => 'filled|integer',
            'nickname' => 'filled|string|max:64',
            'user_type' => 'filled|integer',
            'min_recharge_num' => 'filled|numeric',
            'max_recharge_num' => 'filled|numeric',
            'start_register_time' => 'filled|string',
            'end_register_time' => 'filled|string',
            'start_recharge_time' => 'filled|string',
            'end_recharge_time' => 'filled|string',
            'auth_type' => 'filled|integer',
            'start_add_time' => 'filled|string',
            'end_add_time' => 'filled|string',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
            'reason' => 'string|max:255',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $page = $request->input('page', 1);
        $perPage = $request->input('page_size', 20);
        $type = ErmEnum::GRAY_TYPE;

        $list = app('repo_erm_public_list')->getList($param, $page, $perPage, $type);
        return Response::sendData($list);
    }


    /**
     * @api {get} /erm/gray/list/export/excel 导出灰名单列表
     * @apiVersion 0.1.0
     * @apiName 导出灰名单列表
     * @apiGroup 灰名单管理
     *
     * @apiParam {int} user_code 用户ID，非必传
     * @apiParam {int} nickname 用户昵称，非必传
     * @apiParam {int} user_type 用户类型 1普通用户 2官方管理 3公会会长 4公会厅主 5公会成员，非必传
     * @apiParam {int} min_recharge_money 最大累计充值金额，非必传
     * @apiParam {int} max_recharge_money 最小累计充值金额，非必传
     * @apiParam {int} start_register_time 最大注册时间，非必传
     * @apiParam {int} end_register_time 最小注册时间，非必传
     * @apiParam {int} start_recharge_time 最大最后充值时间，非必传
     * @apiParam {int} end_recharge_time 最小最后充值时间，非必传
     * @apiParam {int} auth_type 认证状态 1充值 2消费 3提现 4兑换，非必传
     * @apiParam {int} start_add_time 最小添加时间，非必传
     * @apiParam {int} end_add_time 最大添加时间，非必传
     * @apiParam {int} page 第几页，默认1
     * @apiParam {int} page_size 每页显示条数，1-100之间，默认10
     * @apiSuccessExample 成功响应
     *  {
     *   "code": 200,
     *   "data": {
     *       "download_url": "asdasdasd.com"
     *       }
     *  }
     */
    public function export(Request $request)
    {
        $params = $request->only('user_code', 'nickname', 'user_type', 'min_recharge_money', 'max_recharge_money', 'start_register_time', 'end_register_time', 'start_recharge_time', 'end_recharge_time', 'auth_type', 'start_add_time', 'end_add_time', 'page', 'page_size');
        $v = app('validator')->make($params, [
            'user_code' => 'filled|integer',
            'nickname' => 'filled|string|max:64',
            'user_type' => 'filled|integer',
            'min_recharge_num' => 'filled|numeric',
            'max_recharge_num' => 'filled|numeric',
            'start_register_time' => 'filled|string',
            'end_register_time' => 'filled|string',
            'start_recharge_time' => 'filled|string',
            'end_recharge_time' => 'filled|string',
            'auth_type' => 'filled|integer',
            'start_add_time' => 'filled|string',
            'end_add_time' => 'filled|string',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $params = $request->input();
        $perPage = 10000;
        $page = 1;
        $type = ErmEnum::GRAY_TYPE;
        $list = app('repo_erm_public_list')->getList($params, $page, $perPage, $type);


        if ($list) {
            $result = [];
            foreach ($list as $item) {
                $temp = [
                    $item->user_code,
                    "\t" . HelperService::wxNickNameFormat($item->nickname),
                    $item->user_type,
                    $item->register_time,
                    $item->register_date,
                    $item->recharge_num,
                    $item->recharge_money,
                    $item->last_recharge_time,
                    $item->join_time,
                    $item->reason,
                    $item->remark,
                    implode(' ', $item->authentication_data),
                ];
                $result[] = $temp;
            }

            $headings = [
                '用户ID',
                '用户昵称',
                '用户类型',
                '注册时间',
                '注册天数',
                '累计充值订单',
                '累计充值金额',
                '最后充值时间',
                '添加时间',
                '加灰原因',
                '备注',
                '认证状态',
            ];

            return app('repo_export')->export(27, $params, $headings, $result);
        }
    }


    /**
     * @api {get} /erm/gray/detail 灰名单详情
     * @apiVersion 0.1.0
     * @apiName 灰名单详情
     * @apiGroup 灰名单管理
     *
     * @apiParam {Int} id id，必传
     *
     * @apiSuccessExample {json} Response-Example
     * {
     * "code": 200,
     * "data": {
     * "detail(详细信息)": {
     * "id": 6,
     * "nickname(用户昵称)": "今天阳光明媚呀",
     * "user_code(用户ID)": 343110,
     * "user_id": 643415,
     * "user_type(用户类型)": "官方管理",
     * "register_time(注册时间)": "2021-11-17 22:04:11",
     * "recharge_num(累计充值订单)": 0,
     * "recharge_money(累计充值金额)": "4000.00",
     * "last_recharge_time(最后充值时间)": "2021-11-17 22:04:11",
     * "gray_num(灰名单次数)": 0,
     * "black_num(黑名单次数)": 0,
     * "register_date(注册天数)": 18957,
     * "join_time(添加时间)": "2021-11-26 16:46:56",
     * "join_date(添加天数)": 1,
     * "join_user(添加人)": "admin",
     * "recharge_auth(充值认证)": {
     * "status(状态 0未认证 1已认证 2认证失败)": 1,
     * "photo(用户照片)": ""
     * },
     * "withdraw_auth": {
     * "status": 0
     * },
     * "consumption_auth": {
     * "status": 2,
     * "photo": ""
     * },
     * "exchange_auth": {
     * "status": 0
     * }
     * },
     * "recharge_list(最近5天充值记录)": [{
     * "id": 291,
     * "money(充值金额)": "0.01",
     * "add_time(充值时间)": "2021-11-17 11:30:32",
     * "merchant_type": 2,
     * "pay_type(支付渠道)": "支付宝",
     * "pay_status(支付状态)": "未支付",
     * "merchant_name(收款商户)": "微信明日"
     * }],
     * "consumption_list(最近5天消费记录)": [{
     * "id": 7357,
     * "user_id": 643415,
     * "user_money(消费金额)": "666666",
     * "change_type": 4,
     * "change_time(消费时间)": "2021-10-29 14:08:45",
     * "change_type_name(消费类型)": "赠送礼物"
     * }],
     * "exchange_list(最近5天兑换记录)": [{
     * "id": 196252,
     * "user_id": 643415,
     * "user_money(兑换金额)": "10000",
     * "change_type": 24,
     * "change_time(兑换时间)": "2021-10-12 13:25:41",
     * "cardholder(提现姓名)": "马宏婷",
     * "bank_num(提现支付宝)": "131481686131",
     * "status(支付状态 1.待审核 2.通过 3.拒绝)": 3,
     * "remark(失败原因)": null,
     * "change_type_name(兑换类型)": "提现失败"
     * }]
     * },
     * "message": ""
     * }
     */
    public function detail(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:0',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $page = 1;
        $perPage = 5;
        $type = ErmEnum::GRAY_TYPE;

        $detail = app('repo_erm_public_list')->getDetail($param, $type);
        $param['user_id'] = $detail['user_id'];
        $recharge_list = app('repo_erm_public_list')->getRechargeList($param, $page, $perPage, $type);
        $consumption_list = app('repo_erm_public_list')->getConsumptionList($param, $page, $perPage, $type);
        $exchange_list = app('repo_erm_public_list')->getExchangeList($param, $page, $perPage, $type);

        $result = [
            'detail' => $detail,
            'recharge_list' => $recharge_list['data'],
            'consumption_list' => $consumption_list['data'],
            'exchange_list' => $exchange_list['data'],
        ];
        return Response::sendData($result);
    }


    /**
     * @api {post} /erm/gray/join/black 加入黑名单
     * @apiVersion 0.1.0
     * @apiName 加入黑名单
     * @apiGroup 灰名单管理
     *
     * @apiParam {Int} id id，必传
     *
     * @apiSuccessExample {json} Response-Example
     * {
     * "code": 200,
     * "data": true,
     * "message": ""
     * }
     */
    public function joinBlack(Request $request)
    {
        $param = $request->only('id', 'prove', 'remark');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:0',
            'prove' => 'string',
            'remark' => 'string|max:30',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $param['old_type'] = ErmEnum::GRAY_TYPE;
        $param['type'] = ErmEnum::BLACK_TYPE;
        $param['admin_id'] = $request->admin_id ?? '';
        $param['remark'] = $param['remark'] ?? '';

        app('repo_erm_public_list')->joinList($param);

        return Response::sendData(true);
    }


    /**
     * @api {get} /erm/gray/detail/recharge/list 最近5天充值记录
     * @apiVersion 0.1.0
     * @apiName 最近5天充值记录
     * @apiGroup 灰名单管理
     *
     * @apiParam {Int} id id，必传
     * @apiParam {int} page 第几页，默认1
     * @apiParam {int} page_size 每页显示条数，1-100之间，默认10
     *
     * @apiSuccessExample {json} Response-Example
     * {
     * "code": 200,
     * "data": {
     * "current_page": 1,
     * "data": [{
     * "id": 291,
     * "money(充值金额)": "0.01",
     * "add_time(充值时间)": "2021-11-17 11:30:32",
     * "merchant_type": 2,
     * "pay_type(支付渠道)": "支付宝",
     * "pay_status(支付状态)": "未支付",
     * "merchant_name(收款商户)": "微信明日"
     * }],
     * "first_page_url": "http://www.qipaoadmin.com/erm/white/detail/recharge/list?=1",
     * "from": 1,
     * "last_page": 1,
     * "last_page_url": "http://www.qipaoadmin.com/erm/white/detail/recharge/list?=1",
     * "next_page_url": null,
     * "path": "http://www.qipaoadmin.com/erm/white/detail/recharge/list",
     * "per_page": 5,
     * "prev_page_url": null,
     * "to": 1,
     * "total": 1
     * },
     * "message": ""
     * }
     */
    public function detailRechargeList(Request $request)
    {
        $param = $request->only('id', 'page', 'page_size');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:0',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $page = $request->input('page', 1);
        $perPage = $request->input('page_size', 5);
        $type = ErmEnum::GRAY_TYPE;

        $public_list_res = app("model_erm_public_list")->where(['type' => $type, 'id' => $param['id']])->first();
        if (!$public_list_res) {
            throw new BasicException(100001);
        }
        $param['user_id'] = $public_list_res['user_id'];
        $recharge_list = app('repo_erm_public_list')->getRechargeList($param, $page, $perPage, $type);

        return Response::sendData($recharge_list);
    }

    /**
     * @api {get} /erm/gray/detail/consumption/list 最近5天消费记录
     * @apiVersion 0.1.0
     * @apiName 最近5天消费记录
     * @apiGroup 灰名单管理
     *
     * @apiParam {Int} id id，必传
     * @apiParam {int} page 第几页，默认1
     * @apiParam {int} page_size 每页显示条数，1-100之间，默认10
     *
     * @apiSuccessExample {json} Response-Example
     * {
     * "code": 200,
     * "data": {
     * "current_page": 1,
     * "data": [{
     * "id": 4112,
     * "user_id": 643415,
     * "user_money(消费金额)": "666666",
     * "change_type": 4,
     * "change_time(消费时间)": "2021-10-29 14:08:45",
     * "change_type_name(消费类型)": "赠送礼物"
     * }],
     * "first_page_url": "http://www.qipaoadmin.com/erm/white/detail/recharge/list?=1",
     * "from": 1,
     * "last_page": 1,
     * "last_page_url": "http://www.qipaoadmin.com/erm/white/detail/recharge/list?=1",
     * "next_page_url": null,
     * "path": "http://www.qipaoadmin.com/erm/white/detail/recharge/list",
     * "per_page": 5,
     * "prev_page_url": null,
     * "to": 1,
     * "total": 1
     * },
     * "message": ""
     * }
     */
    public function detailConsumptionList(Request $request)
    {
        $param = $request->only('id', 'page', 'page_size');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:0',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $page = $request->input('page', 1);
        $perPage = $request->input('page_size', 5);
        $type = ErmEnum::GRAY_TYPE;

        $public_list_res = app("model_erm_public_list")->where(['type' => $type, 'id' => $param['id']])->first();
        if (!$public_list_res) {
            throw new BasicException(100001);
        }
        $param['user_id'] = $public_list_res['user_id'];
        $recharge_list = app('repo_erm_public_list')->getConsumptionList($param, $page, $perPage, $type);

        return Response::sendData($recharge_list);
    }


    /**
     * @api {get} /erm/gray/detail/exchange/list 最近5天兑换记录
     * @apiVersion 0.1.0
     * @apiName 最近5天兑换记录
     * @apiGroup 灰名单管理
     *
     * @apiParam {Int} id id，必传
     * @apiParam {int} page 第几页，默认1
     * @apiParam {int} page_size 每页显示条数，1-100之间，默认10
     *
     * @apiSuccessExample {json} Response-Example
     * {
     * "code": 200,
     * "data": {
     * "current_page": 1,
     * "data": [{
     * "id": 4112,
     * "user_id": 643415,
     * "user_money(兑换金额)": "10000",
     * "change_type": 24,
     * "change_time(兑换时间)": "2021-10-12 13:25:41",
     * "cardholder(提现姓名)": "马宏婷",
     * "bank_num(提现支付宝)": "131481686131",
     * "status(支付状态 1.待审核 2.通过 3.拒绝)": 3,
     * "remark(失败原因)": null,
     * "change_type_name(兑换类型)": "提现失败"
     * }],
     * "first_page_url": "http://www.qipaoadmin.com/erm/white/detail/recharge/list?=1",
     * "from": 1,
     * "last_page": 1,
     * "last_page_url": "http://www.qipaoadmin.com/erm/white/detail/recharge/list?=1",
     * "next_page_url": null,
     * "path": "http://www.qipaoadmin.com/erm/white/detail/recharge/list",
     * "per_page": 5,
     * "prev_page_url": null,
     * "to": 1,
     * "total": 1
     * },
     * "message": ""
     * }
     */
    public function detailExchangeList(Request $request)
    {
        $param = $request->only('id', 'page', 'page_size');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:0',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $page = $request->input('page', 1);
        $perPage = $request->input('page_size', 5);
        $type = ErmEnum::GRAY_TYPE;

        $public_list_res = app("model_erm_public_list")->where(['type' => $type, 'id' => $param['id']])->first();
        if (!$public_list_res) {
            throw new BasicException(100001);
        }
        $param['user_id'] = $public_list_res['user_id'];
        $recharge_list = app('repo_erm_public_list')->getExchangeList($param, $page, $perPage, $type);

        return Response::sendData($recharge_list);
    }

    /**
     * @Desc:灰名单移除记录
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/7 17:28
     */
    public function removeGrayListLog(Request $request)
    {
        $params = $request->only(
            'page',
            'page_size',
            'user_code',
            'nickname',
            'user_type',
            'operator_id',
            'new_type',
            'register_start_time',
            'register_end_time',
            'remove_start_time',
            'remove_end_time',
        );
        $v = app('validator')->make(
            $params,
            [
                'page_size'  => 'filled|numeric',
                'page'       => 'filled|numeric',
                'user_code'  => 'filled|numeric',
                'nickname'   => 'filled|string',
                'user_type'  => 'filled|numeric',
                'operator_id' => 'filled|numeric',
                'new_type'   => 'filled|numeric',
                'register_start_time' => 'filled|date',
                'register_end_time' => 'filled|date',
                'remove_start_time' => 'filled|date',
                'remove_end_time' => 'filled|date',
            ]
        );

        $page = $request->input('page', 1);
        $perPage = $request->input('page_size', 10);

        if ($v->fails()) {
            throw new BasicException(0, getValidateMessage($v->messages()));
        }
        $params['per_page'] = $params['per_page'] ?? 20;
        $params['page'] = $params['page'] ?? 1;

        $recharge_list = app('repo_erm_gray')->removeGrayListLog($params, $page, $perPage);

        return Response::sendData($recharge_list);
    }

    /**
     * @Desc:移除灰名单
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/7 17:28
     */
    public function removeGrayList(Request $request){
        $params = $request->only('id', 'reason', 'remark');
        $v = app('validator')->make($params, [
            'id' => 'required|integer|min:0',
            'reason' => 'required|string',
            'remark' => 'string|max:100',
        ]);
        if ($v->fails()) {
            throw new BasicException(0, getValidateMessage($v->messages()));
        }
        if ($params['reason'] == '其他' && empty($params['remark'])) {
            throw new BasicException(0, '请输入其他原因备注');
        }

        $remark = $params['remark'] ?? '';
        $param['old_type'] = ErmEnum::GRAY_TYPE;
        $param['type'] = ErmEnum::PUBLIC_TYPE;
        $param['admin_id'] = $request->admin_id ?? '';
        $param['id'] = $params['id'];
        $param['remark'] = $params['reason'] . ':' . $remark;
        app('repo_erm_public_list')->joinList($param);

        return Response::sendData(true);
    }


    /**
     * @api {get} /erm/gray/reason/list 加灰原因
     * @apiVersion 1.5.0
     * @apiName 加灰原因
     * @apiGroup 灰名单管理
     */
    public function reasonList(Request $request) {
        $list = app('repo_erm_public_list')->grayReasonList();

        return Response::sendData($list);
    }

}